function menuExited() {
	var topUl = document.getElementById('nav');
	var uls = topUl.getElementsByTagName('ul');
	for ( var i = 0; i < uls.length; i++) {
		ul = uls[i];
		ul.style.left = '-999em';
	}
}

function menuclicked(item) {
	var topUl = document.getElementById('nav');
	var uls = topUl.getElementsByTagName('ul');
	for ( var i = 0; i < uls.length; i++) {
		ul = uls[i];
		ul.style.left = '-999em';
	}
}

function topMenuHover() {
	var topUl = document.getElementById('nav');
	var uls = topUl.getElementsByTagName('ul');
	for ( var i = 0; i < uls.length; i++) {
		ul = uls[i];
		ul.style.left = '';
	}
}
 
function postParams(params) {
	var postForm = document.forms[0]; 
	var paramsIndex = params.split("&");
	for (i = 0; paramsIndex != null &&i < paramsIndex.length; i++) {
		var tmps = paramsIndex[i].split("=");
		var param = document.createElement("input");
		param.type = "hidden";
		param.name = tmps[0];
		param.value = encodeURI(tmps[1]);
		postForm.appendChild(param);
	}
	postForm.submit();
}
 

function setDisableEnable(abc) {
	var x = document.getElementsByTagName("input");
	var textarea = document.getElementsByTagName("textarea");
	var combobox = document.getElementsByTagName("select");
	if (document.getElementById('btnSave') == null) {
		for ( var i = 0; i < x.length; i++) {
			if (x[i].getAttribute("class") == 'readonly') {
				x[i].readOnly = true;
			}
		}
		for ( var i = 0; i < textarea.length; i++) {
			if (textarea[i].getAttribute("class") == 'readonly') {
				textarea[i].readOnly = true;
			}
		}
		for ( var i = 0; i < combobox.length; i++) {
			if (combobox[i].getAttribute("class") == 'readonly') {
				combobox[i].disabled = true;
			}
		}
	}
	if (document.getElementById('btnSave').getAttribute('value') == 'Save') {
		return true;
	}
	var result = true;
	if (abc == 'true') {
		result = false;
		document.getElementById('btnSave').setAttribute('value', 'Save');
	}
	for ( var i = 0; i < x.length; i++) {
		if (x[i].getAttribute("class") == 'readonly') {
			x[i].readOnly = result;
		}
	}
	for ( var i = 0; i < textarea.length; i++) {
		if (textarea[i].getAttribute("class") == 'readonly') {
			textarea[i].readOnly = result;
		}
	}
	for ( var i = 0; i < combobox.length; i++) {
		if (combobox[i].getAttribute("class") == 'readonly') {
			combobox[i].disabled = result;
		}
	}
	return false;
}

function setCheckAll() {
	var check = document.getElementById("checkAll");
	var result = check.checked;
	var x = document.getElementsByTagName("input");
	for ( var i = 0; i < x.length; i++) {
		if (x[i].getAttribute("class") == 'tableCheckBox') {
			x[i].checked = result;
		}
	}
	try {
		var btndelete = document.getElementById('btnDelete');
		if(btndelete != null && "plainbtn" == btndelete.getAttribute("class")){
			btndelete.disabled = !result;
		}
	} catch (e) {
	}
}

function disableCheckAll() {
	var checkAll = document.getElementById("checkAll");
	var x = document.getElementsByTagName("input");
	var test = true;
	for ( var i = 0; i < x.length; i++) {
		if (x[i].getAttribute("class") == 'tableCheckBox' && x[i].checked == false) {
			test = false;
			break;
		}
	}
	checkAll.checked = test;
	try {
		var showBtn = false;
		for ( var i = 0; i < x.length; i++) {
			if (x[i].getAttribute("class") == 'tableCheckBox' && x[i].checked == true) {
				showBtn = true;
				break;
			}
		}
		var btndelete = document.getElementById('btnDelete');
		if(btndelete != null && "plainbtn" == btndelete.getAttribute("class")){
			btndelete.disabled = !showBtn;
		}
	} catch (e) {
	}
}

function linkedComboBox (primaryComboId, secondaryId) {
	var primary = document.getElementById(primaryComboId);
	var value = primary.options[primary.selectedIndex].value;
	
	var secondary = document.getElementById(secondaryId);
	var divHidden = document.getElementById("hiddenDiv");
	if(divHidden == null){
		divHidden = document.createElement('div');
		divHidden.setAttribute("id", "hiddenDiv");
		divHidden.setAttribute("hidden", "true");
		divHidden.innerHTML = secondary.innerHTML;
		var divSundry = document.getElementsByTagName('div')[0];
		divSundry.appendChild(divHidden);
	} 
	var list = divHidden.getElementsByTagName("option");
	var html = "";
	for(var i =0; i < list.length; i ++ ){
		var currentDistCode = list[i].value;
		var currentProvCode = currentDistCode.substring(0, 3);
		if(currentProvCode != value && value != 'All' && currentDistCode != 'All'){
		} else { 
			html += "<option value=\"" + currentDistCode + "\">" + list[i].innerHTML + "</option>\n";
		}
	}
	secondary.innerHTML = html;
}

var req;
var which;  
var availableTags;	
var textId;
function retrieveURL(url , textboxId) {
  textId = textboxId;
  if (window.XMLHttpRequest) { // Non-IE browsers
	    req = new XMLHttpRequest();
	    req.onreadystatechange = processStateChange;
	    try {
		    	var textbox = document.getElementById(textboxId);
		    	req.open("GET", url + "?text=" + textbox.value, true);
	    } catch (e) {
	    }
	    req.send(null);
  } else if (window.ActiveXObject) { // IE
	    req = new ActiveXObject("Microsoft.XMLHTTP");
	    if (req) {
	      req.onreadystatechange = processStateChange;
	      req.open("GET", url, true);
	      req.send();
	    }
  }
}

function processStateChange() {
  if (req.readyState == 4) { // Complete
    if (req.status == 200) { // OK response
    	availableTags = req.responseText.split(";", 1000);
    	$("#" + textId ).autocomplete({
			source: availableTags
		});
    }  
  }
}
 
function sleep(ms)
{
	var dt = new Date();
	dt.setTime(dt.getTime() + ms);
	while (new Date().getTime() < dt.getTime());
}

function rolesProcess(elem){
	var type = elem.getAttribute("name").split("_", 100)[0];
	var parentLi = elem.parentNode;
	var childs = parentLi.getElementsByTagName("input");
	for(var i = 0; childs != null && i < childs.length; i++){
		if(childs[i].getAttribute("name").split("_", 100)[0] == type){
			childs[i].checked = elem.checked;
		}
	}
	if(elem.checked == false){ 
		while (elem) { 
		    elem = elem.parentNode;
		    if(elem.tagName == 'DIV'){
		    	break;
		    }
		    if(elem.tagName == 'LI'){
			    var childs2 = elem.getElementsByTagName("input");
			    for(var i = 0; childs2 != null && i < childs2.length && i < 4; i++){
					if(childs2[i].getAttribute("name").split("_", 100)[0] == type){
						childs2[i].checked = false;
					}
				}
		    }
		}
	} else {
		deQuyRoles(elem, type);
	}
}
 
function deQuyRoles(elem, type){
	    elem = elem.parentNode.parentNode;
	    if(elem == null ||  elem.tagName == 'DIV'){
	    	return;
	    }
	    if(elem.tagName == 'LI' || elem.tagName == 'UL'){
	    	var childs = elem.getElementsByTagName("input");
	    	var allChecked = true;
	    	for(var i = 0; childs != null && i < childs.length; i++){
	    		if(childs[i].getAttribute("name").split("_", 100)[0] == type){
	    			if(childs[i].checked == false){
	    				allChecked = false;
	    				break;
	    			}
	    		}
	    	}
	    	if(allChecked){ 
	    		var childs2 = elem.parentNode.getElementsByTagName("input");
	    		for(var i = 0; childs2 != null && i < childs2.length; i++){
	    			if(childs2[i].getAttribute("name").split("_", 100)[0] == type){
	    				childs2[i].checked = true;
	    				deQuyRoles(elem, type);
	    				break;
	    			}
	    		}
	    	}
	    } 
}

function addQuestion(ele){
	var element = document.getElementById('surveyId');
	var questionOrder = "1";
	for(var i = 1; i < 1000; i ++){
		var tmp = element.getElementById('txtQuestion' + i);
		if(tmp == null || tmp.lengh == 0){
			questionOrder = '' + i;
			break;
		}
	} 
	var divTag = document.createElement('div');
	divTag.setAttribute('id', "questionDiv"  + questionOrder);
	divTag.innerHTML =   "<br/><br/><br/><hr/>" +
										"<label><b>Question:</b></label>" + 
										"<textarea name=\"txtQuestion" + questionOrder + "\" style=\"width: 630px;\" id=\"txtQuestion"  + questionOrder + "\"></textarea>" + 
										"<input id=\"btnRemoveQuestion" + questionOrder + "\" type=\"button\"  value=\"Remove\" onclick=\"removeQuestion(this);\"/>" + 
										"<br/><label></label>" + 
										"<input type=\"radio\" name=\"radio" + questionOrder + "\" value=\"multi\">Multiple response</input>" +
										"<input type=\"radio\" name=\"radio" + questionOrder + "\" value=\"single\" checked>Multiple choice</input><br/>" +
										"<br/><label></label>" + 
										"<input id=\"btnAddAnswer" + questionOrder + "\" type=\"button\"  value=\"Add answer\" onclick=\"addAnswer(this);\"/>";
	element.appendChild( divTag );
}

function addAnswer(ele){
	var idButton = ele.getAttribute('id');
	var questionOrder = idButton.substring(12);
	var divQuestion = document.getElementById('questionDiv' + questionOrder);
	divQuestion.removeChild(ele);
	var answerOrder = "1";
	for(var i = 1; i < 1000; i ++){
		var tmp = divQuestion.getElementById("txtAnswer_" + questionOrder + "_" + i);
		if(tmp == null || tmp.lengh == 0){
			answerOrder = '' + i;
			break;
		}
	}
	var nameAnswer = questionOrder + "_" + answerOrder;
	var divTag = document.createElement('div'); 
	divTag.setAttribute('id', "answerDiv_"  + nameAnswer);
	divTag .innerHTML = "<b>Answer:&nbsp;</b>" + 
			"<textarea name=\"txtAnswer_" + nameAnswer + "\" style=\"width: 350px;height: 25px\" id=\"txtAnswer_" + nameAnswer + "\"></textarea>" +
			"<input type=\"checkbox\" name=\"cbo_" + nameAnswer + "\">Allow Free Text</input>" +
			"&nbsp;&nbsp;&nbsp;<input id=\"answerDiv_" + nameAnswer + "\" type=\"button\"  value=\"Remove\" onclick=\"removeAnswer(this);\"/>" + 
			"<br/><label></label>";
	divQuestion .appendChild(divTag);
	divQuestion .appendChild(ele);
}

function removeQuestion(ele){
	var idButton = ele.getAttribute('id');
	var questionOrder = idButton.substring(17);
	var divQuestion = document.getElementById('questionDiv' + questionOrder);
	if(divQuestion != null){
		var element = document.getElementById('surveyId');
		element.removeChild(divQuestion);
	}
}

function removeAnswer(ele){
	var idButton = ele.getAttribute('id');
	var index = idButton.split('_');
	var divQuestion = document.getElementById('questionDiv' + index[1]);
	var divAnswer = divQuestion.getElementById(idButton);
	if(divQuestion != null && divAnswer != null){
		divQuestion.removeChild(divAnswer);
	}
}

var reqNotify;
function testAJSend(mode){
	 // alert(sessionId + "|" + userId + "|" + empId);
	 if (window.XMLHttpRequest) { // Non-IE browsers
		    reqNotify = new XMLHttpRequest();
		    reqNotify.onreadystatechange = testAJReceive;
		    try {
		    	var url = "GetNotification?mode=" + mode;
		    	reqNotify.open("GET", url , true);
		    } catch (e) {
		    }
		    reqNotify.send(null);
	  }
}

function testAJReceive() {
	  if (reqNotify.readyState == 4) { // Complete
	    if (reqNotify.status == 200) { // OK response
	    	var text = reqNotify.responseText;
	    	if(text != null && text.trim() != ''){
	    		var ies = text.split(';', 20);
	    		// alert(ies);
	    		for(var i = 0; ies != null && i < ies.length; i++){
	    			var elem = document.getElementById(ies[i]);
	    			if(elem != null){
	    				elem.setAttribute("style", "color: red;");
	    				deQuyAJReceive(elem);
	    			}
	    		}
	    	}
	    	testAJSend(1);
	    }  
	  }
}

function deQuyAJReceive(elem) {
	var parent = elem.parentNode;
	if(parent.tagName == 'DIV' || parent.tagName == 'div'){
		return;
	}
	if(parent.tagName == 'LI' || parent.tagName == 'li'){
		var firstATag = parent.getElementsByTagName('a')[0];
		firstATag.setAttribute("style", "font-weight: bold;color: red;");
		if(parent.getAttribute('class') == 'l1'){
			//firstATag.setAttribute("style", "font-weight: bold;");
			var listDivs = parent.getElementsByTagName('div');
			if(listDivs != null && listDivs.length != 0){
				var firstDiv = listDivs[0];
				if(firstDiv != null && firstDiv.getAttribute('class') != null 
						&& firstDiv.getAttribute('class') == 'notification1'){
					parent.removeChild(firstDiv);
				}
			}
			parent.innerHTML = "<div class='notification1'></div>" + parent.innerHTML;
		} else {
			//firstATag.setAttribute("style", "font-weight: bold;color: red;");
		}
	}
	deQuyAJReceive(parent);
}







